package com.golddaymicromall.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.DianZiLvAdapter;
import com.golddaymicromall.base.BaseFragment;
import com.golddaymicromall.bean.ShouYiInfo;
import com.golddaymicromall.bean.ShouYiLvInfo;
import com.golddaymicromall.module.ListViewLoadMore;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by dawd on 2016/3/3.
 */
public class YuFuKuanFragment extends BaseFragment implements ListViewLoadMore.IsLoadingListener {
    private List<ShouYiLvInfo> listAll = new ArrayList<ShouYiLvInfo>();
    private View view;
    private String customerid;
    private List<ShouYiInfo> list;
    private List<ShouYiLvInfo> list1;
    private TextView mnumber;
    private TextView myu_ee;
    private TextView mname;
    private ListViewLoadMore mdianzi_lv;
    private String id;
    private DianZiLvAdapter adapter;
    int CurrentPageNum = 0;
    private Handler handler = new Handler();
    int flag = 1;
    ProgressBar progress;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_yufukuan, container, false);
        customerid = SPUtils.get(getContext(), "Id", "").toString();
        initView();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();

        CurrentPageNum = 1;
        attemptYuFuKuantop(customerid,CurrentPageNum);
    }

    private void attemptYuFuKuanlv(String id,int CurrentPageNum) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.profit.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", id);
        form.put("current_pagenum", String.valueOf(CurrentPageNum));
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> showLoading())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    dismissLoading();
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getActivity(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
//                        Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            list1 = JSON.parseArray(result, ShouYiLvInfo.class);
                            if (CurrentPageNum > 1) {
                                listAll.addAll(list1);
                            } else if (CurrentPageNum == 1) {
                                listAll.clear();
                                listAll.addAll(list1);

                            }
                            adapter = new DianZiLvAdapter(listAll, getActivity());
                            mdianzi_lv.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                            if (CurrentPageNum > 1){
                                mdianzi_lv.setSelection(mdianzi_lv.getCount() - 1);
                            }
                        }
                    } else {
                        Toast.makeText(getActivity(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {

                    throwable.printStackTrace();
                });




    }

    private void attemptYuFuKuantop(String customerid,int CurrentPageNum) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.wallet.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", customerid);
        form.put("wallettype", "4");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getActivity(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            list = JSON.parseArray(result, ShouYiInfo.class);
                            myu_ee.setText("余额   "+list.get(0).getBalance() );
                            attemptYuFuKuanlv(list.get(0).getId() == null ? "" : list.get(0).getId(),CurrentPageNum);
                        }
                    } else {
                        Toast.makeText(getActivity(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {

                    throwable.printStackTrace();
                });



    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onLoad() {
        handler.postDelayed(() -> {
            CurrentPageNum++;
            attemptYuFuKuantop(customerid,CurrentPageNum);
            adapter.notifyDataSetChanged();
            mdianzi_lv.complateLoad();
        }, 3000);


    }




    private void initView() {
        progress = (ProgressBar) view.findViewById(R.id.progress);
        mdianzi_lv = (ListViewLoadMore) view.findViewById(R.id.dianzi_lv2);
        mdianzi_lv.setOnLoadingListener(this);
        myu_ee = (TextView) view.findViewById(R.id.dianzizhanghu_yufukuan_yu_e);

    }


}
